var express = require("express");
var router = express.Router();
const mysql = require("../util/mysql/mysql");

//搜索
router.post("/search", function (req, res, next) {
  if (req.body.good) {
    mysql.data(
      "select * from goods where title like '%" + req.body.good + "'",
      [],
      function (err, rows, fields) {
        if (err) {
          console.log(err);
          return;
        }
        res.send(JSON.stringify(rows));
      }
    );
  } else if (req.body.type) {
    mysql.data(
      "select * from goods where g_type=" + req.body.type,
      [],
      function (err, rows, fields) {
        if (err) {
          console.log(err);
          return;
        }
        res.send(JSON.stringify(rows));
      }
    );
  } else if (req.body.ye) {
    mysql.data("select * from goods limit " + (req.body.ye - 1) * 12 + ",12", [], function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify(rows));
    });
  } else {
    mysql.data("select * from goods", [], function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify(rows));
    });
  }
});
// 首页数据
router.post("/home", function (req, res, next) {
  var prom = [];
  for (var i = 0; i < 5; i++) {
    prom.push(
      new Promise((resolve, reject) => {
        var num = i;
        mysql.data(
          "select * from goods_type where id=" + (num + 1),
          [],
          function (err1, rows1, fields) {
            if (err1) {
              reject(err1);
              return;
            }
            mysql.data(
              "select * from goods where g_type=" + (num + 1) + " limit 4",
              [],
              function (err2, rows2, fields) {
                if (err2) {
                  reject(err2);
                  return;
                }
                var dataobj = { ...rows1[0], goods: [...rows2] };
                resolve(dataobj);
              }
            );
          }
        );
      })
    );
  }
  Promise.all(prom).then((ress) => {
    res.send(JSON.stringify(ress));
  });
});
//商品详情数据
router.post("/goodsinfor", function (req, res, next) {
  mysql.data('SELECT * from recently_browse  where good_id=?', [req.body.id], function (err, rows1, fields) {
    if (err) {
      console.log(err);
      return;
    } else if (rows1.length == 0) {
      mysql.data('insert into recently_browse(good_id) values(?)', [req.body.id], function (err, rows, fields) {
        if (err) {
          console.log(err);
          return;
        }
      });
    }
  });
  mysql.data(
    "SELECT * from goods a INNER JOIN goods_infor b ON b.g_id=a.id where id=" +
    req.body.id,
    [],
    function (err, rows1, fields) {
      if (err) {
        console.log(err);
        return;
      }
      mysql.data(
        "SELECT * from my_collect where good_id=" +
        req.body.id,
        [],
        function (err, rows, fields) {
          console.log(rows);
          if (err) {
            console.log(err);
            return;
          } else if (rows.length == 0) {
            res.send(JSON.stringify({ ...rows1[0], code: 0 }));
          } else {
            res.send(JSON.stringify({ ...rows1[0], code: 1 }));
          }
        }
      );
    }
  );
});
//同城数据
router.post("/mycityData", function (req, res, next) {
  mysql.data(
    "select * from store where city=? limit " +
    (req.body.ye - 1) * 12 +
    ",12",
    [req.body.city],
    function (err, rows1, fields) {
      if (err) {
        console.log(err);
        return;
      }
      mysql.data(
        "select * from store where city=?",
        [req.body.city],
        function (err, rows2, fields) {
          if (err) {
            console.log(err);
            return;
          }
          const citydata = { length: rows2.length, data: rows1 }
          res.send(JSON.stringify(citydata));
        }
      );
    }
  );
});
//星期五商品
router.post("/firdayData", function (req, res, next) {
  mysql.data(
    "select * from goods where g_type=5 limit " +
    (req.body.ye - 1) * 16 +
    ",16",
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify(rows));
    }
  );
});
//积分商城商品
router.post("/jfstoreData", function (req, res, next) {
  mysql.data(
    "select * from goods limit " + (req.body.ye - 1) * 16 + ",16",
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify(rows));
    }
  );
});
//商店详情
router.post("/storeinfor", function (req, res, next) {
  mysql.data(
    "SELECT * from store where s_id=" + req.body.id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify(rows[0]));
    }
  );
});
//添加购物车
router.post("/addShopcar", function (req, res, next) {
  const data = req.body;
  mysql.data(
    "SELECT g_id,g_g from shop_car where g_id=" + data.g_id,
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      if (rows.length == 0) {
        mysql.data(
          "INSERT INTO shop_car(g_id,g_num,user_id,g_g,g_check) VALUES (?,?,?,?,?)",
          [
            data.g_id,
            data.g_num,
            data.u_id,
            data.g_g,
            2
          ],
          function (err, rows, fields) {
            if (err) {
              console.log(err);
              return;
            }
            res.send(JSON.stringify({ code: 1 }));
          }
        );
      } else if (rows[0].g_g == data.g_g) {
        mysql.data(
          "UPDATE shop_car SET g_num=g_num+?,g_check=? where g_id=?",
          [
            data.g_num,
            2,
            data.g_id,
          ],
          function (err, rows, fields) {
            if (err) {
              console.log(err);
              return;
            }
            res.send(JSON.stringify({ code: 1 }));
          }
        );
      } else {
        mysql.data(
          "INSERT INTO shop_car(g_id,g_num,user_id,g_g,g_check) VALUES (?,?,?,?,?)",
          [
            data.g_id,
            data.g_num,
            data.u_id,
            data.g_g,
            2
          ],
          function (err, rows, fields) {
            if (err) {
              console.log(err);
              return;
            }
            res.send(JSON.stringify({ code: 1 }));
          }
        );
      }
    }
  );
});
//购物车发生变化
router.post("/shopcarAdd", function (req, res, next) {
  mysql.data(
    "select * from shop_car where g_check=2 ",
    [],
    function (err, rows, fields) {
      if (err) {
        console.log(err);
        return;
      }
      res.send(JSON.stringify({ code: rows.length }));
    }
  );
});
//获取地址信息
router.post("/getSiteData", function (req, res, next) {
  if (req.body.code == 1) {
    mysql.data(
      "SELECT province,p_id from site_all where type=1",
      [],
      function (err, rows, fields) {
        if (err) {
          console.log(err);
          return;
        }
        res.send(JSON.stringify(rows));
      }
    );
  } else if (req.body.code == 2) {
    mysql.data(
      "SELECT city,c_id from site_all where type=2 and c_p=" + req.body.p_id,
      [],
      function (err, rows, fields) {
        if (err) {
          console.log(err);
          return;
        }
        res.send(JSON.stringify(rows));
      }
    );
  } else if (req.body.code == 3) {
    mysql.data(
      "SELECT district,d_id from site_all where type=3 and d_c=" + req.body.c_id,
      [],
      function (err, rows, fields) {
        if (err) {
          console.log(err);
          return;
        }
        res.send(JSON.stringify(rows));
      }
    );
  }

});
module.exports = router;
